package com.me.blood.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.me.blood.databinding.ActivityWebBinding;
import com.me.utils.Utils;
import com.plata.base.utils.WeakHandler;
import com.me.ad.ADType;

public class WebActivity extends BaseActivity {

    ActivityWebBinding binding;
    WeakHandler h = new WeakHandler();

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        binding = ActivityWebBinding.inflate(getLayoutInflater());
        binding.root.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        WebSettings settings = binding.web.getSettings();
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setDomStorageEnabled(true);//是否支持 Local Storage
        settings.setJavaScriptEnabled(true);//支持JavaScript
        settings.setUseWideViewPort(true);//使Webview具有普通的视口(例如普通的桌面浏览器)
        settings.setLoadWithOverviewMode(true);//加载完全缩小的WebView
        settings.setSupportZoom(true);// 设置可以支持缩放
        settings.setBuiltInZoomControls(true);// 设置启用缩放功能

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            settings.setMediaPlaybackRequiresUserGesture(false);
        }
        binding.web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    if (start) {
                        start = false;
                    }
                    binding.progress.setVisibility(View.GONE);
                } else {
                    if (!start) {
                        start = true;
                    }
                    binding.progress.setVisibility(View.VISIBLE);
                    binding.progress.setProgress(newProgress);
                }
            }
        });
        binding.web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                String scheme = uri.getScheme();
                if (scheme.equals("http") || scheme.equals("https")) {
                    return false;
                } else {
                    toSysWeb(uri);
                    return true;
                }
            }
        });

        String url = getIntent().getStringExtra("url");
        settings.setDisplayZoomControls(true);//显示缩放控制按钮
        binding.web.loadUrl(url);
    }

    boolean start = false;

    public static void launch(Context c, String url) {
        Intent i = new Intent(c, WebActivity.class);
        i.putExtra("url", url);
        if (c instanceof Activity == false) {
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        c.startActivity(i);
    }

    private void toSysWeb(Uri uri) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void goBack() {
        if (binding.web.canGoBack()) {
            binding.web.goBack();
        } else {
            finish();
        }
    }

    @Override
    protected ADType getXpType() {
        return ADType.XP;
    }

    @Override
    protected void onDestroy() {
        Utils.freeWeb(binding.web);
        super.onDestroy();
    }
}
